from seasun.model import get_connection
from seasun.model.xiaohua import Xiaohua

# 获取数据库连接
_db = get_connection()

def insertxiaohua(title: str, text: str, status = 1) ->None:
    '''
    将笑话插入数据库
    :param title:
    :param text:
    :param status:
    :return:
    '''
    xiao = Xiaohua(title=title, text=text, status=status)
    _db.add(xiao)
    _db.commit()

def selectfirst():
    '''
    查询出第一条未发送过状态的笑话（状态为1）
    :return:
    '''
    xiaohua = _db.query(Xiaohua).filter(Xiaohua.status == 1).first()
    return xiaohua

def updatexiaohua_status(id: int) -> None:
    '''
    更新笑话发送状态（更改为0，表示已发送过）
    :param id:
    :return:
    '''
    _db.query(Xiaohua).filter_by(id=id).update({'status': 0})
    _db.commit()

def selectxiaohua():
    a = _db.query(Xiaohua).filter(Xiaohua.id == 1).first()

    print(a.status)
    print(a.text)
    # count = 0
    # for a in all:
    #     if len(a.text) > 3420:
    #         print(a.id)
    #         count = count + 1
    #
    # print(count)
